package com.feynman.pattern.singleton.lazy;

/**
 * @ClassName LazyInnerClassSingleton
 * @Description 静态内部类单例模式
 * @Author Feynman
 * @date 2019/4/4 20:43
 * @Version V1.0
 **/
public class LazyInnerClassSingleton {

    //虽然构造方法私有了
    private LazyInnerClassSingleton(){
        if(LazyHolder.lazy != null){
            throw new RuntimeException("不能创建多个实例");
        }
    }

    //懒汉式单例
    //LazyHolder里面的逻辑需要等到外部方法调用时才执行
    //巧妙的利用了内部类的特性
    //JVM底层执行逻辑，完美避免了线程安全的问题
    public static final LazyInnerClassSingleton getInstance(){
        return LazyHolder.lazy;
    }

    //默认不加载
    private static class LazyHolder{
        private static final LazyInnerClassSingleton lazy = new LazyInnerClassSingleton();
    }
}
